/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-06-29     Rbb666       first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#include "drv_gpio.h"

#define LED_PIN      GET_PIN(0, 0)
#define LED_PIN1     GET_PIN(0, 1)
#define SDIO_CD1     GET_PIN(2, 6)

rt_uint8_t *Buf = NULL;


void MainShowData()
{
    int inx = 0;
    rt_kprintf("test-> sd data is :");
    while(inx <= 510){
        rt_kprintf("0x%X ",Buf[inx]);
        inx++;
    }
    rt_kprintf("0x%X\n",Buf[511]);
}

void sdrd(int argc, char**argv)
{
    int addr = 0;
    addr = atoi(argv[1]);
    rt_kprintf("hello sdread! paraCnt[%d]: addr[%d]\n",argc,addr);
    rt_device_t sdSt = rt_device_find("sd0");
    rt_device_open(sdSt,RT_DEVICE_OFLAG_RDWR);
    rt_memset(Buf, 0, 512);
    rt_device_read(sdSt,addr,Buf,1);
    MainShowData();
    rt_device_close(sdSt);
}
MSH_CMD_EXPORT(sdrd , para:1 addr);
void sdwt(int argc, char**argv)
{
    int addr = 0;
    int val = 0;
    addr = atoi(argv[1]);
    val = atoi(argv[2]);
    rt_kprintf("hello sdwrite paraCnt[%d]: addr[%d]val[%d]\n",argc,addr,val);
    rt_device_t sdSt = rt_device_find("sd0");
    rt_device_open(sdSt,RT_DEVICE_OFLAG_RDWR);
    rt_memset(Buf, val, 512);
    rt_device_write(sdSt,addr,Buf,1);
    rt_device_close(sdSt);
}
MSH_CMD_EXPORT(sdwt , para:1 addr;2 val);
void GpioHdrIrp(void *args)
{
    rt_kprintf("GpioHdrIrp\n");
}
int main(void)
{
    Buf = rt_malloc(512);
    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(LED_PIN1, PIN_MODE_OUTPUT);
    rt_pin_irq_enable(SDIO_CD1,PIN_IRQ_ENABLE);
    rt_pin_attach_irq(SDIO_CD1,PIN_IRQ_MODE_RISING,GpioHdrIrp,RT_NULL);
    for (;;)
    {
        rt_pin_write(LED_PIN, PIN_HIGH);
        rt_pin_write(LED_PIN1, PIN_LOW);
        rt_thread_mdelay(500);
        rt_pin_write(LED_PIN, PIN_LOW);
        rt_pin_write(LED_PIN1, PIN_HIGH);
        rt_thread_mdelay(500);
        //rt_kprintf("hello get pin value!SDIO_CD1[%d]\n",rt_pin_read(SDIO_CD1));
    }
}
